<?php
class Card extends CI_Controller {
	private $image;
	function Card() {
		parent::__construct ();
		$username = $this->session->userdata('username');
		$level = $this->session->userdata('level');
		if($username !='' && $level == 1 ){
			
		}
		else
		{
			redirect(base_url().'index.php/user');
		}
		$this->load->model ( 'be/card_model' );
		$this->load->model ( 'be/cardtype_model' );
		$this->load->library ( 'form_validation' );
	}
	
	function index() {
		$data ['list'] = $this->card_model->get ();
		$data ['page_title'] = 'Danh sách thẻ';
		$data ['main_content'] = 'be/card/list';
		$this->load->view ( 'be/includes/template', $data );
	}
	function add() {
		$this->form_validation->set_rules ( 'name', 'Tên card', 'required' );
		$this->form_validation->set_rules ( 'image', 'Nguồn', 'callback_get_image_uploaded' );
		
		if ($this->form_validation->run () == FALSE) {
			$data['types'] = $this->cardtype_model->get();
			$data ['page_title'] = 'Thêm mới quảng cáo';
			$data ['main_content'] = 'be/card/add';
			$this->load->view ( 'be/includes/template', $data );
		} else {
			$data['name'] = $this->input->post('name');
			$data['price'] = $this->input->post('price');
			$data['type'] = $this->input->post('type');
			$data['status'] = $this->input->post('status');
			$data ['image'] = $this->image;
			
			if (! $this->card_model->add ( $data )) {
				$data ['page_title'] = 'Có lỗi xảy ra';
				$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/card/add"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/card">Trở lại trang danh sách</a></p>';
				$data ['message_detail'] = $mess_detail;
			} else {
				$data ['page_title'] = 'Thành công';
				$mess_detail = '<p>Cập nhật thành công!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/card/add"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/card">Trở lại trang danh sách</a></p>';
				$data ['message_detail'] = $mess_detail;
			}
			$data ['main_content'] = 'be/customer/message';
			$this->load->view ( 'be/includes/template', $data );
		}
	}
	
	function update($id) {
		$card = $this->card_model->get ( $id );
		$this->form_validation->set_rules ( 'image', 'Nguồn', 'callback_get_image_uploaded' );
		if($this->input->post('update')){
			$data['id'] = $id;
			$data['name'] = $this->input->post('name');
			$data['price'] = $this->input->post('price');
			$data['type'] = $this->input->post('type');
			$data['status'] = $this->input->post('status');
			$data ['image'] = $this->input->post ( 'image' );
			if($this->image == CUSTOMER_DEFAULT_AVATAR ) {
				$data ['image'] = $card ['image'];
			}else {
				$data['image'] = $this->source;
			}
			
			$result = $this->card_model->update($id,$data);
			if($result)
			redirect(base_url() . 'index.php/card');
			else
			echo "Can't update this card";
		}else{
			$card = $this->card_model->get($id);
			$data['ct'] = $this->cardtype_model->get();
			$data['card'] = $card;
			

			if($card['image'] == null)$card['image'] = "images/nophoto.jpg";
			if(!$card) redirect(base_url().'admin/managecard');
			$data['card'] = $card;
			$data['pageinfo'] = 'Edit "'.$card['name'].'" card';
	
			$data ['page_title'] = 'Cập nhật lại thẻ';
			$data ['main_content'] = 'be/card/update';
			$this->load->view ( 'be/includes/template', $data );
		}
	}
	
	function get_image_uploaded() {
		//Create location if it does not exist
		$location = DEFAULT_IMAGE_UPLOAD_PATH . '/card';
		if (! is_dir ( $location )) {
			if (! mkdir ( $location, 0755 )) {
				die ( 'Failed to create folders...' );
			}
		}
		$config ['upload_path'] = $location;
		$config ['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config ['max_size'] = '10000';
		
		$this->load->library ( 'upload', $config );
		if ($this->upload->do_upload ( 'source' )) {
			$tmp = $this->upload->data ( 'source' );
			$this->source = $location . '/' . $tmp ['file_name'];
		} else {
			$this->source = CUSTOMER_DEFAULT_AVATAR;
		}
	}
	
	function delete() {
		$ids = $this->input->post ( 'selected' );
		$flag = true;
		foreach ( $ids as $id ) {
			if (! $this->card_model->delete ( $id )) {
				$flag = false;
			}
		}
		
		if (! $flag) {
			//echo "Delete not successfully!";
		}
		
		redirect ( site_url ( 'index.php/card' ) );
	}
}
?>